<?php
abstract class collection_collect_abstract implements collection_collect_interface {

	protected $_http = null;
	protected $_row = null;
	protected $_rule = array();
	protected $_currentPage = 0;
	protected $_pages = array();
	protected $_error = array();
	
	public function __construct($row) {
		$this->_http = new collection_collect_http();
		$this->_row = $row;
		try {
			if(!$this->_row) {
				throw new collection_exception("RULE ROW IS EMPTY");
			}
			$this->_rule = @unserialize($row->get('rule'));
		} catch (Exception $e) {
			$this->_rule = array();
		}
	}
	
	public function __toString() {
		return serialize($this->_rule);
	}
		
	public function __call($method, array $args) {
		if(preg_match('/^get(\w+?)$/', $method, $matches)) {
			//默认：true
			$def = isset($args[0]) ? $args[0] : null;
			$name = preg_replace('/([A-Z]+)/s', '_$1', $matches[1]);
			return $this->get(trim($name, '_'), $def);
		}
		return null;
	}
	
	public function get($name, $def = null) {
		$name = strtolower($name);
		if(isset($this->_rule[$name])) {
			return $this->_rule[$name];
		}
		return $def;
	}
	
	public function setFullLink($link) {
		if(!$this->isLink($link)) {
			$base = preg_replace('/[\\\\]+/is', '/', $this->_row->getUrl());
			$_link = preg_replace('/[\\\\]+/is', '/', $link);
			// 根路径
			if(preg_match('/^\/(.*)$/is', $_link)) { 
				$url = parse_url($base);
				$_http = $url['scheme'] . '://';
				if(isset($url['user'])) $_http .= $url['user'];
				if(isset($url['pass'])) $_http .= ':' . $url['pass'];
				if(isset($url['user']) || isset($url['pass'])) $_http .= '@';
				$_http .= $url['host'];
				if(isset($url['port'])) $_http .= ':' . $url['port'];
				return $_http . $_link;
			} else {
				$lurl = substr($base, 0, strrpos($base, '/'));
				return $lurl . '/' . $_link;
			}			
		}
		return $link;
	}
	
	public function isLink($link) {
		return preg_match('/^(http|https|ftp)\:\/\/[a-zA-Z0-9]{1,}(.*)$/is', $link);
	}
	
	protected function getResponse($url = null) {
		$_url = ($url === null) ? $this->_row->getUrl() : $url;
		if(!$_url) {
			throw new collection_exception("TARGET Link is Empty.");
		}
		if(!$this->isLink($_url)) {
			throw new collection_exception("TARGET Link is Empty or not a correct protocol.");
		}
		$http = $this->getHttp();
		$response = $http->get($_url);
		$http->cleanCookieJar();
		if(!$http->getIsOk()) {
			throw new collection_exception("HTTP RESPONSE IS NOT OK: $url");
		}
		return mb_convert_encoding($response, 'UTF-8', $this->_row->getCharset());
	}
	/**
	 * @return collection_collect_http
	 */
	protected function getHttp() {
		return $this->_http;
	}

	public function Collect() {}
	
	public function getPages() {
		return $this->_pages;
	}
	/**
	 * @return collection_collect_abstract
	 */
	public function setPages(array $pages) {
		$this->_pages = $pages;
		return $this;
	}
	
	/**
	 * @return collection_collect_abstract
	 */
	public function setPage($page) {
		$this->_currentPage = $page;
		return $this;
	}
	
	public function getPage() {
		return $this->_currentPage;
	}
	
	public function getCurrentUrl() {
		$p = $this->getPage();
		return isset($this->_pages[$p]) ? $this->_pages[$p] : '';
	}
	
	/**
	 * @return collection_collect_abstract
	 */
	public function addError($error) {
		$this->_error[] = $error;
		return $this;
	}
	
	public function getError() {
		return $this->_error;
	}
	
}
